package com.xtoad.ecms.baseinfo.controller;

import com.xtoad.ecms.baseinfo.dto.MenuDTO;
import com.xtoad.ecms.baseinfo.service.IMenuService;
import com.xtoad.ecms.common.web.annotation.ResponseAdvice;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 菜单类控制器
 *
 * @author xtoad
 * @date 2021/01/18
 */
@RestController
@RequestMapping("menu")
@ResponseAdvice
public class MenuController {

    @Resource
    private IMenuService menuService;

    /**
     * 根据id查询菜单类信息
     *
     * @param id 菜单类id
     * @return 查询结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public MenuDTO getMenuById(@PathVariable final Long id) {
        MenuDTO menu = menuService.getMenuById(id);

        return menu;
    }

    /**
     * 查询全部菜单类信息，不分页
     *
     * @return 全部菜单类信息
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public List<MenuDTO> getAllMenu() {
        List<MenuDTO> menus = menuService.getAllMenu();

        return menus;
    }

    /**
     * 新增菜单类
     *
     * @param menuDTO 菜单类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MenuDTO insertMenu(@Valid @RequestBody final MenuDTO menuDTO) {
        MenuDTO insMenu = menuService.insert(menuDTO);

        return insMenu;
    }

    /**
     * 更新菜单类信息
     *
     * @param menuDTO 更新内容
     * @return 更新结果
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public MenuDTO updateMenu(@PathVariable final Long id, @Valid @RequestBody final MenuDTO menuDTO) {
        menuDTO.setId(id);
        MenuDTO updMenu = menuService.update(menuDTO);

        return updMenu;
    }

    /**
     * 删除菜单类信息
     *
     * @param id 删除对象id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Boolean deleteMenu(@PathVariable final Long id) {
        menuService.deleteById(id);

        return true;
    }

    /**
     * 批量新增
     *
     * @param menus 菜单类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/add", method = RequestMethod.POST)
    public List<MenuDTO> batchAddMenu(@Valid @RequestBody final List<MenuDTO> menus) {
        List<MenuDTO> insMenus = menuService.batchInsert(menus);

        return insMenus;
    }

    /**
     * 批量更新
     *
     * @param menus 菜单类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/modify", method = RequestMethod.POST)
    public List<MenuDTO> batchUpdMenu(@Valid @RequestBody final List<MenuDTO> menus) {
        List<MenuDTO> updMenus = menuService.batchUpdate(menus);

        return updMenus;
    }

    /**
     * 批量删除
     *
     * @param menus 菜单类信息
     * @return 更新结果
     */
    @RequestMapping(value = "/batch/delete", method = RequestMethod.POST)
    public Boolean batchDelMenu(@Valid @RequestBody final List<MenuDTO> menus) {
        menuService.batchDelete(menus);

        return true;
    }
}
